{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ipywidgets"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 材料力学莫尔圆\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "0f59e929f653454e8a779e3df8c1d4a8",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "interactive(children=(IntSlider(value=25, description='s_x', min=-50), IntSlider(value=25, description='s_y', …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from numpy import *\n",
    "from matplotlib.pyplot import *  \n",
    "from ipywidgets import interactive\n",
    "def stress(s_x,s_y,shear):\n",
    "    z=linspace(0,360,360)\n",
    "    R=sqrt(((s_x-s_y)/2)**2+shear**2)\n",
    "    X=(s_x+s_y)/2+R*cos(radians(z))\n",
    "    Y=R*sin(radians(z))\n",
    "    C_X=([s_x,s_x,s_y,s_y,s_x])\n",
    "    C_Y=([0,shear,-shear,0,0])\n",
    "    plot(C_X,C_Y,X,Y)\n",
    "    xlabel(\"Sigma\")\n",
    "    ylabel(\"Shear Stress\")\n",
    "    axis('equal')\n",
    "    show()\n",
    "interactive_plot=interactive(stress,shear=(0,90),s_x=(-50,100),s_y=(-50,100))\n",
    "interactive_plot\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "18ebfd617f7c4482a232bd9407cb27a5",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "IntSlider(value=0)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import ipywidgets as widgets\n",
    "from IPython.display import display\n",
    "w = widgets.IntSlider()\n",
    "display(w)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['_dom_classes',\n",
       " '_model_module',\n",
       " '_model_module_version',\n",
       " '_model_name',\n",
       " '_view_count',\n",
       " '_view_module',\n",
       " '_view_module_version',\n",
       " '_view_name',\n",
       " 'continuous_update',\n",
       " 'description',\n",
       " 'description_tooltip',\n",
       " 'disabled',\n",
       " 'layout',\n",
       " 'max',\n",
       " 'min',\n",
       " 'orientation',\n",
       " 'readout',\n",
       " 'readout_format',\n",
       " 'step',\n",
       " 'style',\n",
       " 'value']"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w.keys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "a901fd02df314b92ad4e8db05bd54124",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Text(value='Hello World!', disabled=True)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "widgets.Text(value='Hello World!', disabled=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "19d94fe1be49433fa8b77f771abef1c8",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "FloatText(value=0.0)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d3d69063e31d441cb8566d34acb1d2c0",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "FloatSlider(value=0.0)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "a = widgets.FloatText()\n",
    "b = widgets.FloatSlider()\n",
    "display(a,b)\n",
    "\n",
    "mylink = widgets.jslink((a, 'value'), (b, 'value'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "60383401a6f346998305bf9b7baaac30",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Output(layout=Layout(border='1px solid black'))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "out = widgets.Output(layout={'border': '1px solid black'})\n",
    "out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "with out:\n",
    "    for i in range(10):\n",
    "        print(i, 'Hello world!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.display import YouTubeVideo\n",
    "with out:\n",
    "    display(YouTubeVideo('XDhJ8lVGbl8'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "b0aef6fb004e477ab0a77c1e04d38b92",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(VBox(children=(IntSlider(value=0, description='a'), IntSlider(value=0, description='b'), IntSli…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "a = widgets.IntSlider(description='a')\n",
    "b = widgets.IntSlider(description='b')\n",
    "c = widgets.IntSlider(description='c')\n",
    "def f(a, b, c):\n",
    "    print('{}*{}*{}={}'.format(a, b, c, a*b*c))\n",
    "\n",
    "out = widgets.interactive_output(f, {'a': a, 'b': b, 'c': c})\n",
    "\n",
    "widgets.HBox([widgets.VBox([a, b, c]), out])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Utils widgets\n",
    "from ipywidgets import Button, Layout, jslink, IntText, IntSlider\n",
    "\n",
    "def create_expanded_button(description, button_style):\n",
    "    return Button(description=description, button_style=button_style, layout=Layout(height='auto', width='auto'))\n",
    "\n",
    "top_left_button = create_expanded_button(\"Top left\", 'info')\n",
    "top_right_button = create_expanded_button(\"Top right\", 'success')\n",
    "bottom_left_button = create_expanded_button(\"Bottom left\", 'danger')\n",
    "bottom_right_button = create_expanded_button(\"Bottom right\", 'warning')\n",
    "\n",
    "top_left_text = IntText(description='Top left', layout=Layout(width='auto', height='auto'))\n",
    "top_right_text = IntText(description='Top right', layout=Layout(width='auto', height='auto'))\n",
    "bottom_left_slider = IntSlider(description='Bottom left', layout=Layout(width='auto', height='auto'))\n",
    "bottom_right_slider = IntSlider(description='Bottom right', layout=Layout(width='auto', height='auto'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "eab0c131863d4a498ddbe9c9043aa06a",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "TwoByTwoLayout(children=(Button(button_style='info', description='Top left', layout=Layout(grid_area='top-left…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from ipywidgets import TwoByTwoLayout\n",
    "\n",
    "TwoByTwoLayout(top_left=top_left_button,\n",
    "               top_right=top_right_button,\n",
    "               bottom_left=bottom_left_button,\n",
    "               bottom_right=bottom_right_button)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.10.4 ('python310')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "a888cd810c90be07c3c921372a6d7ff433f5a38d5779dfb2a871d99db75673fb"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
